import {createStoreBindings} from "mobx-miniprogram-bindings"
const dateutil=require("../../utils/date");
import {store} from "../../store/store"
import axios from "../../utils/axios";
// pages/myactivity/myactivity.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbarlist:["已报名","我发布的","审核中","未通过"],
    signupactivities:[],
    signupproject:[],
    myactivities:[],
    myprojects:[],
    failactivities:[],
    failprojects:[],
    auditactivities:[],
    auditprojects:[],
    currenttap:0,
  },
 async gettabbarindex(e){
    this.setData({
      currenttap:e.currentTarget.dataset.index
    })
    if(this.data.currenttap==0){
    this.getsignup()
    }else{
      if(this.data.currenttap==1){
        this.getmyactivities()
      }else{
        if(this.data.currenttap==2){
          this.getaudit()
        }else{
         this.getfail()
        }
      }
    }
   console.log(this.data)
  },
 async getsignup(){
    var token=getApp().globalData.accessToken
    var activities=[]
    var result= await axios.get('/activity/self/sign')
    console.log(result)
    for (let i = 0; i < result.length; i++) {
      const item = result[i];
      item.endmonth=new Date(item.end).getMonth()+1
      item.enddate=new Date(item.end).getDate()
      var end=new Date(item.end).getTime()
      var now=new Date().getTime()
     var delinestr= dateutil.datetodelinestr(now,end,"剩余","")
     item.delinestr=delinestr
    activities.push(item)
  }
  var projects=[]
  var result=await axios.get("/project/self/sign")
  for (let i = 0; i < result.length; i++) {
    const item = result[i];
    item.endmonth=new Date(item.end).getMonth()+1
    item.enddate=new Date(item.end).getDate()
  var end=new Date(item.end).getTime()
  var now=new Date().getTime()
 item.delinestr= dateutil.datetodelinestr(now,end,"剩余","")
  projects.push(item)
  }
  console.log(result)
  this.setData({
    signupactivities:activities,
    signupproject:projects
  })
  console.log(this.data.signupactivities)
  },
  async getmyactivities(){
    var token=getApp().globalData.accessToken
    var activities=[]
    var result=await axios.get("/activity/self?passed=1")
      for (let i = 0; i < result.length; i++) {
        const item = result[i];
        item.endmonth=new Date(item.end).getMonth()+1
        item.enddate=new Date(item.end).getDate()
      var end=new Date(item.end)
      var now=new Date()
     item.delinestr=dateutil.datetodelinestr(now,end,"剩余","")
      activities.push(item)
    }
      var token=getApp().globalData.accessToken
      var result1=await axios.get("/project/self?passed=1")
      var projects=[]
      for (let i = 0; i < result1.length; i++) {
        const item = result1[i];
        item.endmonth=new Date(item.end).getMonth()+1
        item.enddate=new Date(item.end).getDate()
      var end=new Date(item.end)
      var now=new Date()
     item.delinestr= dateutil.datetodelinestr(now,end,"剩余","")
      projects.push(item)
      }
      this.setData({
        myactivities:activities,
        myprojects:projects
      })
  },
  async getfail(){
    var token=getApp().globalData.accessToken
          var activities=[]
          var result=await axios.get("/activity/self?passed=-1")
        for (let i = 0; i < result.length; i++) {
          const item = result[i];
          item.endmonth=new Date(item.end).getMonth()
          item.enddate=new Date(item.end).getDate()
        var end=new Date(item.end)
        var now=new Date()
      item.delinestr=dateutil.datetodelinestr(now,end,"剩余","")
        activities.push(item)
      }
        var token=getApp().globalData.accessToken
        var result1=await axios.get('/project/self?passed=-1')
        var projects=[]
        for (let i = 0; i < result1.length; i++) {
          const item = result1[i];
          item.endmonth=new Date(item.end).getMonth()+1
          item.enddate=new Date(item.end).getDate()
        var end=new Date(item.end)
        var now=new Date()
        item.delinestr= dateutil.datetodelinestr(now,end,"剩余","")
          projects.push(item)
        }
        console.log(activities)
        this.setData({
          failactivities:activities,
          failprojects:projects          
        })
  },
  async getaudit(){
    var token=getApp().globalData.accessToken
    var activities=[]
    var result=await axios.get("/activity/self?passed=0")
  for (let i = 0; i < result.length; i++) {
    const item = result[i];
    item.endmonth=new Date(item.end).getMonth()+1
    item.enddate=new Date(item.end).getDate()
    item.remindmanager=false
  var end=new Date(item.end)
  var now=new Date()
  item.delinestr=dateutil.datetodelinestr(now,end,"剩余","")
  activities.push(item)
}
var result1=await axios.get('/project/self?passed=0')

  var projects=[]
  for (let i = 0; i < result1.length; i++) {
    const item = result1[i];
    item.endmonth=new Date(item.end).getMonth()+1
    item.enddate=new Date(item.end).getDate()
    item.remindmanager=false
  var end=new Date(item.end)
  var now=new Date()
  item.delinestr=dateutil.datetodelinestr(now,end,"剩余","")
  projects.push(item)
  }
  this.setData({
    auditactivities:activities,
    auditprojects:projects
  })
  },
 async cancel(e){
    var that=this
    wx.showModal({
      title:"确定取消报名吗?",
      success:async function(res){
        if(res.cancel){
          console.log("取消成功")
        }else{
          var token=getApp().globalData.accessToken
          var id=e.currentTarget.dataset.id
          var type=e.currentTarget.dataset.type
          console.log("id",id)
          console.log("type",type)
          switch(type){
            case 0:
              console.log(type)
              await axios.del('/activity/signDown?activityId='+id)
              await that.getsignup()
              break
            case 1:
              await axios.post('/project/signDown?projectId='+id,{})
              await that.getsignup()
              break
          }
        }
      }
    })
  },
  showdetail:function(e){
    var type=e.currentTarget.dataset.type
    var id=e.currentTarget.dataset.id
    var url=""
    switch(type){
      case 0:url="../activitydetail/activitydetail?fail=false&id="+id
      break
      case 1:url="../projectdetail/projectdetail?fail=false&id="+id
    }
    wx.navigateTo({
      url:url
    })  
  },
  showfaildetail:function(e){
    var type=e.currentTarget.dataset.type
    var id=e.currentTarget.dataset.id
    var url=""
    switch(type){
      case 0:url="../activitymodify/activitymodify?fail=true&id="+id
      break
      // case 1:url="../changeproject/changeproject?fail=true&id="+id
    }
    wx.navigateTo({
      url:url
    })  
  },
  manager:function(e){
    var id=e.currentTarget.dataset.id
    var type=e.currentTarget.dataset.type
    var url=""
    switch(type){
      case 0:url="../activitymanager/activitymanager?type=0&id="+id
      break
      case 1:url="../activitymanager/activitymanager?type=1&id="+id
    }
    wx.navigateTo({
      url:url
    })  
  },
  delete:function(e){
    var that=this
wx.showModal({
  title:"确定删除当前项目吗",
  success:function(res){
    if(res.confirm){
      var id=that.data.activities[e.currentTarget.dataset.index].id
      var project=that.data.activities[e.currentTarget.dataset.index].project
      if(project){
      }else{
      }
      that.data.activities.splice(e.currentTarget.dataset.index,1)
    that.setData({
      activities:that.data.activities
    })
    }
    console.log(that.data.activities)
  }
})
},

modify:function(e){
  console.log(e)
  var index=e.currentTarget.dataset.index
  var b=e.currentTarget.dataset.fail
  var project=this.data.activities[index].project
  createStoreBindings(this,{
    store,
    fields:["project"],
    actions:['updateproject']
  })
  store.updateproject(this.data.activities[index])
  if(project){
    wx.navigateTo({
      url:"../changeproject/changeproject?fail="+b
    })
  }else{
    wx.navigateTo({
      url:"../activitymodify/activitymodify?fail="+b
    })
  }
},
remindmanager:function(e){
  var index=e.currentTarget.dataset.index
  var type=e.currentTarget.dataset.type
  var key=""
  switch(type){
    case 0:
      key+="auditactivities["
      break
    case 1:
      key+="auditprojects["
  }
  key+=index+"].remindmanager"
  this.setData({
    [key]:true
  })
  console.log(this.data.auditactivities)
  console.log(this.data.auditprojects)
},
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad() {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.showLoading({
      title:"加载中..",
      mask:true
    })
   this.getsignup()
   console.log(this.data.signupactivities)
   console.log(this.data.signupproject)
    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})